package service.impl;

import java.util.ArrayList;
import java.util.List;

import model.Result;
import model.UserCourse;

import org.json.simple.JSONArray;
import org.json.simple.JSONValue;

public class JSONDecoder {

	public static List<UserCourse> getUserCourses(String text, Long userId, Long semesterId) {
		List<UserCourse> userCourses = new ArrayList<UserCourse>();
		JSONArray data = (JSONArray) JSONValue.parse(text);

		for (int i = 0; i < data.size(); i++) {
			JSONArray array = (JSONArray) data.get(i);
			UserCourse userCourse = new UserCourse();
			userCourse.setCourseCode((String) array.get(1));
			userCourse.setSemesterId(semesterId);
			userCourse.setUserId(userId);
			userCourses.add(userCourse);
		}
		return userCourses;
	}

	public static List<Result> getUserResults(String text, Long userId) {
		List<Result> userResults = new ArrayList<Result>();
		JSONArray data = (JSONArray) JSONValue.parse(text);

		for (int i = 0; i < data.size(); i++) {
			JSONArray array = (JSONArray) data.get(i);
			Result userResult = new Result();
			userResult.setUserId(userId);
			userResult.setAnswerTime(Converter.intoDate((String) array.get(0)));
			userResult.setCourseCode((String) array.get(1));
			userResult.setLearningMethodId(Long.parseLong((String) array.get(2)));
			//FIXME if comma?
			userResult.setStudyPeriod(Double.parseDouble((String) array.get(3)));
			userResults.add(userResult);
		}
		return userResults;
	}


}
